package com.car.web.controller.finance;

import java.io.File;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.car.dal.dao.reception.TgReceptionDao;
import com.car.dal.object.*;
import com.car.service.money.MoneyService;
import com.car.util.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.car.dal.dao.method.TgMethodDao;
import com.car.dal.dao.moneys.TgMoneyDao;
import com.car.dal.dao.office.TgOfficeDao;
import com.car.dal.dao.pos.TgPosDao;
import com.car.dal.dao.reception.ZhichuDao;
import com.car.dal.dao.sudent.StudentDao;
import com.car.dal.dao.user.UserinfoDao;
import com.car.exception.BaseException;
import com.car.util.JsonExecutor.JsonBiz;
import com.car.util.WebExecutor.Biz;
import com.car.web.controller.base.BaseController;
import com.car.web.controller.base.BaseVo;
import com.car.web.controller.index.LoginVo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/index")
public class MoneyfController extends BaseController {

    private static final Logger logger = Logger
            .getLogger(MoneyfController.class);
    @Resource
    private UserinfoDao userinfoDao;
    @Resource
    private TgOfficeDao tgOfficeDao;
    @Resource
    private TgMethodDao methodDao;
    @Resource
    private ZhichuDao zhuchuDao;
    @Resource
    private TgPosDao posDao;
    @Resource
    private TgMoneyDao moneyDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TgReceptionDao receptionDao;
    @Resource
    private MoneyService moneyService;

    //添加中心支付方式
    @RequestMapping(value = "/add_method")
    public ModelAndView add_method(ModelMap modelMap, final LoginVo loginVo,
                                   HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, loginVo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true); 
                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                result.setDefaultModel("offList", offList);
                return result;
            }
        });
        return new ModelAndView("system/finance/add_method", modelMap);
    }

    //异步查询中心付费方式
    @RequestMapping(value = "/get_method")
    public ModelAndView get_method(ModelMap modelMap, final BaseVo vo,
                                   final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center_id = request.getParameter("center_id");
                TgMethod method = new TgMethod();
                if (center_id != null && !center_id.equals("")) {
                    method.setCenter_id(Long.valueOf(center_id));
                    List<TgMethod> list = methodDao.findByExample(method);
                    result.setDefaultModel("list", list);
                }
                return result;
            }

        });
        return new ModelAndView("", modelMap);
    }

    //异步添加中心付费方式
    @RequestMapping(value = "/add_method_go")
    public ModelAndView add_method_go(ModelMap modelMap, final BaseVo vo,
                                      final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center_id = request.getParameter("center_id");
                String name = request.getParameter("name");
                String bank_id = request.getParameter("bank_id");
                TgMethod method = new TgMethod();
                if (center_id != null && !center_id.equals("") &&
                        name != null && !name.equals("")) {

                    method.setCenter_id(Long.valueOf(center_id));
                    method.setName(name);

                    List<TgMethod> list = methodDao.findByExample(method);
                    if (list != null && list.size() > 0) {
                        result.setDefaultModel("msg", "0");
                    } else {
                        method.setBank_id(bank_id);
                        method.setCreate_time(new Date());
                        Long id = methodDao.insert(method);
                        result.setDefaultModel("id", id);
                        result.setDefaultModel("msg", "1");
                    }
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //异步删除中心付费方式
    @RequestMapping(value = "/delete_method")
    public ModelAndView delete_method(ModelMap modelMap, final BaseVo vo,
                                      final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");
                if (id != null && !id.equals("")) {
                    methodDao.delete(TgMethod.class, Long.valueOf(id));
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //进入添加中心支出列表
    @RequestMapping(value = "/zhichuList")
    public ModelAndView zhichuList(ModelMap modelMap, final BaseVo vo,
                                   final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center = request.getParameter("center");

                TgUser user = UserUtil.getUserFromCookies();

                TgZhichu zhichu = new TgZhichu();
                zhichu.setUsername(user.getUsername());
                if (center != null && !"".equals(center)) {
                    zhichu.setRegionns(center);
                }

                Integer currPage = 1;
                if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
                    currPage = vo.getCurrPage();
                }
                Integer total = zhuchuDao.getZcCountListByType(zhichu);
                Page page = PageUtil.createPage(8, currPage, total);
                List<TgZhichu> list = zhuchuDao.getZhichuListByType(zhichu, page);
                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                result.setDefaultModel("offList", offList);
                result.setDefaultModel("zhichu", zhichu);
                result.setDefaultModel("list", list);
                result.setDefaultModel("page", page);
                return result;
            }
        });
        return new ModelAndView("system/finance/zhuchu_moneylist", modelMap);
    }

    //进入添加中心支出
    @RequestMapping(value = "/add_zhichu_moneys")
    public ModelAndView add_zhichu_moneys(ModelMap modelMap, final LoginVo loginVo,
                                          final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, loginVo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");
                if (id != null && !"".equals(id)) {
                    TgZhichu zhichu = zhuchuDao.findById(TgZhichu.class, Long.valueOf(id));
                    result.setDefaultModel("zhichu", zhichu);
                }

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                result.setDefaultModel("offList", offList);
                return result;
            }
        });
        return new ModelAndView("system/finance/add_zhichu_moneys", modelMap);
    }

    //添加中心支出
    @RequestMapping(value = "/add_zhichu_moneys_go")
    public ModelAndView add_zhichu_moneys_go(ModelMap modelMap, final BaseVo vo,
                                             final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");
                    String moneyDate = request.getParameter("moneyDate");
                    String zhaiyao = request.getParameter("zhaiyao");
                    String center_id = request.getParameter("center_id");
                    String method_id = request.getParameter("method_id");
                    String moneys = request.getParameter("moneys");
                    String beizhu = request.getParameter("beizhu");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    TgUser user = UserUtil.getUserFromCookies();

                    TgZhichu zhichu = new TgZhichu();
                    if (moneyDate != null && !"".equals(moneyDate) &&
                            center_id != null && !"".equals(center_id) &&
                            method_id != null && !"".equals(method_id)) {
                        zhichu.setUsername(user.getUsername());
                        zhichu.setMoneyDate(sdf.parse(moneyDate));
                        zhichu.setZhaiyao(zhaiyao);
                        zhichu.setCenter_id(Long.valueOf(center_id));
                        zhichu.setMethod_id(Long.valueOf(method_id));
                        zhichu.setMoneys(Double.valueOf(moneys));
                        zhichu.setBeizhu(beizhu);

                        if (id != null && !"".equals(id)) {
                            zhichu.setId(Long.valueOf(id));
                            zhuchuDao.update(zhichu);
                        } else {
                            zhichu.setCreate_time(new Date());
                            zhuchuDao.insert(zhichu);
                        }
                        result.setDefaultModel("msg", "1");
                    } else {
                        result.setDefaultModel("msg", "0");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    @RequestMapping(value = "/addpos")
    public ModelAndView addpos(ModelMap modelMap, final BaseVo vo,
                               final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);

                String id = request.getParameter("id");
                if (id != null && !"".equals(id)) {
                    TgPos pos = posDao.findById(TgPos.class, Long.valueOf(id));
                    result.setDefaultModel("pos", pos);
                }

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                TgUserinfo user = new TgUserinfo();
                List<TgUserinfo> uList = userinfoDao.getUserInfoByUserInfo(user);

                result.setDefaultModel("offList", offList);
                result.setDefaultModel("uList", uList);
                return result;
            }
        });
        return new ModelAndView("system/finance/add_pos", modelMap);
    }

    //添加pos机
    @RequestMapping(value = "/add_pos_go")
    public ModelAndView add_pos_go(ModelMap modelMap, final BaseVo vo,
                                   final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");
                    String center_id = request.getParameter("center_id");
                    String sh_id = request.getParameter("sh_id");
                    String xyk_fl = request.getParameter("xyk_fl");
                    String jjk_fl = request.getParameter("jjk_fl");
                    String pos_sj = request.getParameter("pos_sj");
                    String bank_id = request.getParameter("bank_id");

                    String yyzz = request.getParameter("yyzz");
                    String id_card = request.getParameter("id_card");
                    String images = request.getParameter("images");
                    String username = request.getParameter("username");
                    String phone = request.getParameter("phone");
                    String xiane = request.getParameter("xiane");
                    String msg = request.getParameter("msg");
                    String state = request.getParameter("state");
                    String banli_date = request.getParameter("banli_date");

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    TgPos pos = new TgPos();
                    if (center_id != null && !"".equals(center_id)) {
                        pos.setCenter_id(Long.valueOf(center_id));
                    }

                    pos.setSh_id(sh_id);
                    pos.setXyk_fl(xyk_fl);
                    pos.setJjk_fl(jjk_fl);
                    pos.setPos_sj(pos_sj);
                    pos.setBank_id(bank_id);
                    pos.setYyzz(yyzz);
                    pos.setId_card(id_card);
                    pos.setImages(images);
                    pos.setUsername(username);
                    pos.setPhone(phone);
                    pos.setXiane(xiane);
                    pos.setMsg(msg);
                    if (state != null && !"".equals(state)) {
                        pos.setState(Integer.valueOf(state));
                    }
                    if (banli_date != null && !"".equals("banli_date")) {
                        pos.setBanli_date(sdf.parse(banli_date));
                    }

                    if (id != null && !"".equals(id)) {
                        pos.setId(Long.valueOf(id));
                        posDao.update(pos);
                    } else {
                        pos.setCreate_time(new Date());
                        posDao.insert(pos);
                    }
                    result.setDefaultModel("msg", "1");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //POS机列表
    @RequestMapping(value = "/posList")
    public ModelAndView posList(ModelMap modelMap, final BaseVo vo,
                                final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center = request.getParameter("center");

                TgPos pos = new TgPos();
                if (center != null && !"".equals(center)) {
                    pos.setCenter(center);
                }

                Integer currPage = 1;
                if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
                    currPage = vo.getCurrPage();
                }
                Integer total = posDao.getPosCountByType(pos);
                Page page = PageUtil.createPage(8, currPage, total);
                List<TgPos> list = posDao.getPosListByType(pos, page);

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                result.setDefaultModel("offList", offList);
                result.setDefaultModel("pos", pos);
                result.setDefaultModel("list", list);
                result.setDefaultModel("page", page);
                return result;
            }
        });
        return new ModelAndView("system/finance/poslist", modelMap);
    }

    //缴费列表
    @RequestMapping(value = "/moneyList")
    public ModelAndView moneyList(ModelMap modelMap, final BaseVo vo,
                                  final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center = request.getParameter("center");
                String stu_name = request.getParameter("stu_name");
                String state = request.getParameter("state");
                String nickname1 = request.getParameter("nickname1");
                String pra_type = request.getParameter("pra_type");
                String startTime = request.getParameter("startTime");
                String endTime = request.getParameter("endTime");
                String mark = request.getParameter("mark");
                
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                TgUser user = UserUtil.getUserFromCookies();

                TgMoney money = new TgMoney();
                if (!user.getUsername().equals("system")) {
                    if (user.getMenu() != null && !"".equals(user.getMenu())) {
                        if (!user.getMenu().contains("14")) {
                            if (user.getMenu().contains("y")) {
                                money.setJd_center_key(user.getRegion());
                            } else if(user.getUsername().equals("xuetingting")){
                            	money.setUsername("");
                            }else{
                                money.setUsername(user.getUsername());
                            }
                        }

                    } else {
                        money.setUsername(user.getUsername());
                    }
                }
                result.setDefaultModel("state", state);
                result.setDefaultModel("startTime", startTime);
                result.setDefaultModel("endTime", endTime);
                if (startTime != null && !"".equals(startTime)) {
                    try {
                        money.setStartTime(sdf.format(sdf.parse(startTime)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (endTime != null && !"".equals(endTime)) {
                    try {
                        money.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(sdf.format(sdf.parse(endTime))))));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (center != null && !"".equals(center) && !"undefined".equals(center)) {
                    money.setJd_center_key(center);
                }
                if (stu_name != null && !"".equals(stu_name)) {
                    money.setStu_name(stu_name);
                }
                if (state != null && !"".equals(state)) {
                    money.setState(Integer.valueOf(state));
                }
                if (nickname1 != null && !"".equals(nickname1)) {
                    money.setNickname1(nickname1);
                }
                if (pra_type != null && !"".equals(pra_type)) {
                    money.setPra_type(Integer.valueOf(pra_type));
                }
                if("1".equals(mark)){
                   money.setIsSuccess(1); 
                   money.setIs_jg("1");
                }
                Integer currPage = 1;
                if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
                    currPage = vo.getCurrPage();
                }
                Integer total = moneyDao.getMonCountByType(money);
                Page page = PageUtil.createPage(8, currPage, total);
                List<TgMoney> list = moneyDao.getMoneysByType(money, page);
                if (list != null) {
                    Gson gson = new Gson();
                    for (TgMoney m : list) {
                        //联系方式
                        if (m.getPhone() != null && !"".equals(m.getPhone())) {
                            if (m.getPhone().contains("[{")) {
                                List<Phone> phone1 = gson.fromJson(m.getPhone(),
                                        new TypeToken<List<Phone>>() {
                                        }.getType());
                                m.setPhlist(phone1);
                            }
                        }
                    }

                }

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                result.setDefaultModel("offList", offList);
                result.setDefaultModel("mark",mark);
                result.setDefaultModel("money", money);
                result.setDefaultModel("list", list);
                result.setDefaultModel("page", page);
                return result;
            }
        });
        if("1".equals(modelMap.get("mark"))){
            return new ModelAndView("system/finance/moneyList_jg", modelMap);

        }else{
            return new ModelAndView("system/finance/moneyList", modelMap);
 
        }
    }

    //下载缴费列表
    @RequestMapping(value = "/moneyListXz")
    public ModelAndView moneyListXz(ModelMap modelMap, final BaseVo vo,
                                    final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center = request.getParameter("center");
                String stu_name = request.getParameter("stu_name");
                String state = request.getParameter("state");
                String nickname1 = request.getParameter("nickname1");
                String pra_type = request.getParameter("pra_type");
                String startTime = request.getParameter("startTime");
                String endTime = request.getParameter("endTime");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                TgUser user = UserUtil.getUserFromCookies();

                TgMoney money = new TgMoney();
                if (!user.getUsername().equals("system")) {
                    if (user.getMenu() != null && !"".equals(user.getMenu())) {
                        if (!user.getMenu().contains("14")) {
                            if (user.getMenu().contains("y")) {
                                money.setJd_center_key(user.getRegion());
                            } else {
                                money.setUsername(user.getUsername());
                            }
                        }

                    } else {
                        money.setUsername(user.getUsername());
                    }
                }

                result.setDefaultModel("startTime", startTime);
                result.setDefaultModel("endTime", endTime);
                if (startTime != null && !"".equals(startTime)) {
                    try {
                        money.setStartTime(sdf.format(sdf.parse(startTime)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (endTime != null && !"".equals(endTime)) {
                    try {
                        money.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(sdf.format(sdf.parse(endTime))))));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (center != null && !"".equals(center) && !"undefined".equals(center)) {
                    money.setJd_center_key(center);
                }
                if (stu_name != null && !"".equals(stu_name)) {
                    money.setStu_name(stu_name);
                }
                if (state != null && !"".equals(state)) {
                    money.setState(Integer.valueOf(state));
                }
                if (nickname1 != null && !"".equals(nickname1)) {
                    money.setNickname1(nickname1);
                }

                if (pra_type != null && !"".equals(pra_type)) {
                    money.setPra_type(Integer.valueOf(pra_type));
                }
                Integer currPage = 1;
                if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
                    currPage = vo.getCurrPage();
                }
                Integer total = moneyDao.getMonCountByType(money);
                Page page = PageUtil.createPage(1000, currPage, total);
                List<TgMoney> list = moneyDao.getMoneysByType(money, page);
                if (list != null) {
                    Gson gson = new Gson();
                    for (TgMoney m : list) {
                        //联系方式
                        if (m.getPhone() != null && !"".equals(m.getPhone())) {
                            if (m.getPhone().contains("[{")) {
                                List<Phone> phone1 = gson.fromJson(m.getPhone(),
                                        new TypeToken<List<Phone>>() {
                                        }.getType());
                                m.setPhlist(phone1);
                            }
                        }
                    }

                }

                String name = WriterExcel.writeExcelMoneyByTime(list, startTime, endTime);
                result.setDefaultModel("name", name);
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //添加缴费
    @RequestMapping(value = "/addMoenys")
    public ModelAndView addMoenys(ModelMap modelMap, final BaseVo vo,
                                  final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);

                String id = request.getParameter("id");
                String stu_id = request.getParameter("stu_id");

                if (id != null && !"".equals(id)) {
                    TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
                    result.setDefaultModel("money", money);
                } else {
                    if (stu_id != null && !"".equals(stu_id)) {
                        TgStudent student = studentDao.findById(TgStudent.class, Long.valueOf(stu_id));
                        result.setDefaultModel("student", student);
                    }
                }

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                TgUserinfo users = new TgUserinfo();
                users.setLocked(0);
                users.setJob("课程顾问");
                List<TgUserinfo> uList = userinfoDao.getUserInfoByUserInfo(users);

                String url = "http://www.yueqiao.org/web/teaweb/getTeacherList.json";
                String teacher = HttpUtil.doGet(url, "", "utf-8", true);
                net.sf.json.JSONArray obj = new net.sf.json.JSONArray();
                obj = JSONObject.fromObject(teacher).getJSONArray("list");
                List<TgUser> list = new ArrayList<TgUser>();
                for (int i = 0; obj.size() > i; i++) {
                    TgUser user = new TgUser();
                    JSONObject o = JSONObject.fromObject(obj.get(i));
                    user.setNickname(o.getString("name"));
                    user.setUsername(o.getString("login_name"));
                    user.setRegion(o.getString("center"));
                    list.add(user);
                }
                result.setDefaultModel("list", list);
                result.setDefaultModel("offList", offList);
                result.setDefaultModel("uList", uList);
                return result;
            }
        });
        return new ModelAndView("system/finance/add_money", modelMap);
    }
    /**
     * @Title: 方法名：add_PZ_picture
     * @Description: 功能描述：添加上传凭证
     * @author hqD ☺ ^o^
     * @date 2017年12月22日 下午4:52:33
     * @param @param modelMap
     * @param @param vo
     * @param @param request
     * @param @return
     * @param @throws Exception    
     * @return ModelAndView    返回类型
     */
    @RequestMapping(value = "/add_PZ_picture")
    public ModelAndView add_PZ_picture(ModelMap modelMap, final BaseVo vo, final HttpServletRequest request)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");
                if (MathUtil.isNotEmpty(id)) {
                    TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
                    result.setDefaultModel("money", money);
                }
                return result;
            }
        });
        return new ModelAndView("system/finance/add_PZ_picture", modelMap);
    }
    
    
    //快速查找训练老师列表
    @RequestMapping(value = "/getTeaList")
    public ModelAndView getTeaList(ModelMap modelMap, final BaseVo vo,
       final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                	String nickname = request.getParameter("nickname");
                	if(nickname!=null && !"".equals(nickname)){
                		nickname=URLEncoder.encode(nickname, "utf-8");
                		String url = "http://www.yueqiao.org/web/teaweb/getTeacherList.json?nickname="+nickname;
                        String teacher = HttpUtil.doGet(url, "", "utf-8", true);
                        net.sf.json.JSONArray obj = new net.sf.json.JSONArray();
                        obj = JSONObject.fromObject(teacher).getJSONArray("list");
                        List<TgUser> list = new ArrayList<TgUser>();
                        for (int i = 0; obj.size() > i; i++) {
                            TgUser user = new TgUser();
                            JSONObject o = JSONObject.fromObject(obj.get(i));
                            user.setNickname(o.getString("name"));
                            user.setUsername(o.getString("login_name"));
                            user.setRegion(o.getString("center"));
                            list.add(user);
                        }
                        result.setDefaultModel("list", list);
                	}
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //添加缴费
    @RequestMapping(value = "/add_money_go")
    public ModelAndView add_money_go(ModelMap modelMap, final BaseVo vo,
         final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");
                    String stu_id = request.getParameter("stu_id");
                    String student = request.getParameter("student");
                    String jd_username1 = request.getParameter("jd_username1");
                    String jd_username2 = request.getParameter("jd_username2");
                    String record_teacher = request.getParameter("record_teacher");
                    String jd_center_key = request.getParameter("jd_center_key");
                    String pra_type = request.getParameter("pra_type");
                    String fw_url = request.getParameter("fw_url");

                    String times1 = request.getParameter("times1");
                    String times2 = request.getParameter("times2");
                    String s_pra = request.getParameter("s_pra");
                    String moneyType1 = request.getParameter("moneyType1");
                    String moneyType2 = request.getParameter("moneyType2");
                    String method_id = request.getParameter("method_id");
                    String moneys = request.getParameter("moneys");
                    String beizhu = request.getParameter("beizhu");

                    String pz_url = request.getParameter("pz_url");

                    String sj_num = request.getParameter("sj_num");
                    String smf_c = request.getParameter("smf_c");
                    String smf_m = request.getParameter("smf_m");
                    String state = request.getParameter("state");
                    String moneyDate = request.getParameter("moneyDate");

                    String fp_moneys = request.getParameter("fp_moneys");
                    String fp_tt = request.getParameter("fp_tt");
                    String is_otkj = request.getParameter("is_otkj");
                    String is_zqkj = request.getParameter("is_zqkj");
                    String is_jj = request.getParameter("is_jj");
                    String jj_msg = request.getParameter("jj_msg");
                    String is_fapiao = request.getParameter("is_fapiao");
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    TgUser user = UserUtil.getUserFromCookies();

                    if (pra_type != null && !"".equals("pra_type") &&
                            method_id != null && !"".equals("method_id") &&
                            moneys != null && !"".equals("moneys") &&
                            moneyDate != null && !"".equals("moneyDate") &&
                            stu_id !=null && !"".equals(stu_id)) {

                    	List<String> dlist = DateUtil.printWeekdays(sdf.format(new Date()));
                    	Long stime = sdf.parse(dlist.get(0)).getTime()/1000;
                    	Long etime = (DateUtil.getTommorrowDay1(sdf.parse(dlist.get(dlist.size()-1)))).getTime()/1000;
                    	
                    	if(sdf.parse(moneyDate).getTime()/1000>=stime && sdf.parse(moneyDate).getTime()/1000<etime){

    	                    TgMoney money = new TgMoney();
    	                    if (fp_moneys != null && !"".equals(fp_moneys)) {
    	                        money.setFp_moneys(Double.valueOf(fp_moneys));
    	                    }
    	                    if (fp_tt != null && !"".equals(fp_tt)) {
    	                        money.setFp_tt(fp_tt);
    	                    }
    	                    if (fw_url != null && !"".equals(fw_url)) {
    	                        money.setFw_url(fw_url);
    	                    }
    	                    
    	                    if (is_otkj != null && !"".equals(is_otkj)) {
    	                        money.setIs_otkj(Integer.valueOf(is_otkj));
    	                    }
    	                    if (is_zqkj != null && !"".equals(is_zqkj)) {
    	                        money.setIs_zqkj(Integer.valueOf(is_zqkj));
    	                    }
    	                    if (is_jj != null && !"".equals(is_jj)) {
    	                        money.setIs_jj(Integer.valueOf(is_jj));
    	                    }
    	                    if (jj_msg != null && !"".equals(jj_msg)) {
    	                        money.setJj_msg(jj_msg);
    	                    }
    	                    if (is_fapiao != null && !"".equals(is_fapiao)) {
    	                        money.setIs_fapiao(Integer.valueOf(is_fapiao));
    	                    }
    	                    
    	
    	                    money.setSj_num(sj_num);
    	                    money.setSmf_c(smf_c);
    	                    money.setSmf_m(smf_m);
    	                    money.setBeizhu(beizhu);
    	                    money.setUsername(user.getUsername());
    	                    money.setJd_username1(jd_username1);
    	                    money.setJd_username2(jd_username2);
    	                    money.setRecord_teacher(record_teacher);
    	                    money.setStudent(student);
    	                    money.setJd_center_key(jd_center_key);
    	                    money.setPra_type(Integer.valueOf(pra_type));
    	                    if (pra_type.equals("5")) {
    	                        money.setTimes(Integer.valueOf(times2));
    	                    } else {
    	                        money.setTimes(Integer.valueOf(times1));
    	                    }
    	                    money.setS_pra(s_pra);
    	                    if (moneyType1 != null && !"".equals(moneyType1)) {
    	                        money.setMoneyType1(Integer.valueOf(moneyType1));
    	                    }
    	                    if (moneyType1 != null && !"".equals(moneyType1)) {
    	                        money.setMoneyType2(Integer.valueOf(moneyType2));
    	                    }
    	                    money.setMethod_id(Long.valueOf(method_id));
    	                    money.setMoneys(Double.valueOf(moneys));
    	                    money.setPz_url(pz_url);
    	
    	                    if (moneyDate != null && !"".equals("moneyDate")) {
    	                        money.setMoneyDate(moneyDate);
    	                    }
    	
    	                    if (id != null && !"".equals(id)) {
    	                        if (state != null && !"".equals(state)) {
    	                            money.setState(Integer.valueOf(state));
	                            	money.setIsSuccess(Integer.valueOf(0));//是否成单
    	                        }
    	                        money.setId(Long.valueOf(id));
    	                        TgMoney m = moneyDao.findById(TgMoney.class, Long.valueOf(id));
    	                        if (m.getState() == 0 || m.getState() == 1) {
    	                            moneyDao.update(money);
    	                            result.setDefaultModel("msg", "1");
    	                        } else {
    	                            result.setDefaultModel("msg", "0");
    	                        }
    	
    	                    } else {
    	                        if (stu_id != null && !"".equals(stu_id)) {
    	                            money.setStu_id(Long.valueOf(stu_id));
    	                        }
    	                        money.setCreate_time(new Date());
    	                        moneyDao.insert(money);
    	                        result.setDefaultModel("msg", "1");
    	                    }
                    	}else{
                    		result.setDefaultModel("msg", "2");
                    	}
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }
    /**
     * @Title: 方法名：add_PZ_picture_go
     * @Description: 功能描述：补传凭证
     * @author hqD ☺ ^o^
     * @date 2017年12月22日 下午5:09:10
     * @param @param modelMap
     * @param @param vo
     * @param @param request
     * @param @param response
     * @param @return
     * @param @throws BaseException    
     * @return ModelAndView    返回类型
     */
    @RequestMapping(value = "/add_PZ_picture_go")
    public ModelAndView add_PZ_picture_go(ModelMap modelMap, final BaseVo vo,final HttpServletRequest request)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");
                    String fw_url = request.getParameter("fw_url");
                    String pz_url = request.getParameter("pz_url");
                    System.out.println("fw_url ="+fw_url);
    	            if (MathUtil.isNotEmpty(id)) {
    	                 TgMoney m = moneyDao.findById(TgMoney.class, Long.valueOf(id));
    	                 if (m.getState() == 0 || m.getState() == 1) {
    	                	 if (MathUtil.isNotEmpty(fw_url)) {
     	                        m.setFw_url(fw_url);
     	                     }
    	                	 if (MathUtil.isNotEmpty(pz_url)) {
      	                        m.setPz_url(pz_url);
      	                     }
    	                     moneyDao.update(m);
    	                     result.setDefaultModel("msg", "1");
    	                 } else {
    	                     result.setDefaultModel("msg", "0");
    	                 }
                    }
                } catch (Exception e) { e.printStackTrace();}
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }
    
    //添加缴费
    @RequestMapping(value = "/updateZonMoney")
    public ModelAndView updateZonMoney(ModelMap modelMap, final BaseVo vo,
       final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");
                String fp_moneys = request.getParameter("fp_moneys");
                TgMoney money = new TgMoney();
                if(id!=null && !"".equals(id)){
                	money.setId(Long.valueOf(id));
                	money.setFp_moneys(Double.valueOf(fp_moneys));
                    moneyDao.update(money);
                    result.setDefaultModel("mgs", "1");
                }
                
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //缴费审核页
    @RequestMapping(value = "/moenysDetail")
    public ModelAndView getmoenysDetail(ModelMap modelMap, final BaseVo vo,
                                        final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);

                String id = request.getParameter("id");
                String fp = request.getParameter("fp");

                if (id != null && !"".equals(id)) {
                    TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
                    result.setDefaultModel("money", money);
                }

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                TgUserinfo users = new TgUserinfo();
                users.setLocked(0);
                users.setJob("课程顾问");
                List<TgUserinfo> uList = userinfoDao.getUserInfoByUserInfo(users);

                result.setDefaultModel("offList", offList);
                result.setDefaultModel("uList", uList);
                result.setDefaultModel("fp", fp);
                return result;
            }
        });
        return new ModelAndView("system/finance/add_money_view", modelMap);
    }

    //缴费审核
    @RequestMapping(value = "/money_state")
    public ModelAndView money_state(ModelMap modelMap, final BaseVo vo,
    		final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
	                String id = request.getParameter("id");
	                String state = request.getParameter("state");
	                String state_beizhu = request.getParameter("state_beizhu");
	                String jg_type = request.getParameter("jg_type");         
	                TgUser user = UserUtil.getUserFromCookies();

	                if (id != null && !"".equals(id) &&
	                		state != null && !"".equals(state) ) {
	                	TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
	                	if(state.equals("2")){
	                		if(money.getStu_id()!=null){
	                			TgStudent student = studentDao.getStudentById(money.getStu_id());
	                			String name="";
	                			if(student==null){
	                				TgStudent stu = new TgStudent(); 
	                				stu.setNickname(money.getStu_name());
	                				List<TgStudent> list = studentDao.findByExample(stu);
	                				if(list!=null && list.size()==1){
	                					student = list.get(0);
	                					name=list.get(0).getNickname();
	    								if(name!=null && !"".equals(name) && !"null".equals(name)){
	    									name = URLEncoder.encode(name, "utf-8");
	    								}else{
	    									name = "";
	    								}
	                				}
	                			}else{
	                				name=student.getNickname();
									if(name!=null && !"".equals(name) && !"null".equals(name)){
										name = URLEncoder.encode(student.getNickname(), "utf-8");
									}else{
										name = "";
									}
	                			}
	                			String center = student.getCenter();
	                			if(student.getCenter().equals("成都桐梓(zǐ)林中心")){
	        						center = "成都桐梓林中心";
	        					}else if(center.equals("广州区(ōu)庄中心")){
	        						center = "广州区庄中心";
	        					}
								center = URLEncoder.encode(center, "utf-8");
								
								String is_taiyou="0";
								if(student.getPra_type()!=null && student.getPra_type()==4){
									is_taiyou = "1";
								}
								
								String situation="";
								if(student.getPra_type()!=null){
									String s_pra="";
									if(student.getS_pra()==null || "".equals(student.getS_pra())){
										s_pra = "无";
									}else{
										s_pra = student.getS_pra();
									}
									if(student.getPra_type()==1){
										situation="非脑电"+student.getTimes()+"次，赠送课时："+s_pra;
									}else if(student.getPra_type()==2){
										situation="普通中心"+student.getTimes()+"次，赠送课时："+s_pra;
									}else if(student.getPra_type()==3){
										situation="全脑"+student.getTimes()+"次，赠送课时："+s_pra;
									}else if(student.getPra_type()==4){
										situation="太优"+student.getTimes()+"次，赠送课时："+s_pra;
									}else if(student.getPra_type()==5){
										situation="家庭训练"+student.getTimes()+"月，赠送课时："+s_pra;
									}else if(student.getPra_type()==6){
										situation="夏令营";
									}else if(student.getPra_type()==7){
										situation="上门费，赠送课时："+s_pra;
									}else if(student.getPra_type()==8){
										situation="暑期培优班(新)"+student.getTimes()+"次，赠送课时："+s_pra;
									}else if(student.getPra_type()==9){
										situation="暑期培优班(旧)，赠送课时："+s_pra;
									}
								}
	        					if(situation!=null && !"".equals(situation)){
	        						situation = URLEncoder.encode(situation, "utf-8");
	        					}
	                    		String url = "http://www.yueqiao.org/web/teaweb/addStudent.json?id="+student.getId()+"&name="+name+"&center="+center+"&is_taiyou="+is_taiyou+"&situation="+situation;
	        	                String teacher = HttpUtil.doGet(url, "", "utf-8", true);
	        	                JSONObject obj = JSONObject.fromObject(teacher);
	        	                String msg = obj.getString("msg");
	        	                if(msg!=null && msg.equals("1")){
	        	                	money.setState(Integer.valueOf(state));
	        	                    money.setState_name(user.getNickname());
	        	                    money.setState_beizhu(state_beizhu);
	        	                    money.setJg_type(jg_type);
	        	                    moneyDao.update(money);
	        	                    result.setDefaultModel("msg", "1");
	        	                }
	                		}else{
	    	                	money.setState(Integer.valueOf(state));
	    	                    money.setState_name(user.getNickname());
	    	                    money.setState_beizhu(state_beizhu);
		                        money.setJg_type(jg_type);
	    	                    moneyDao.update(money);
	    	                    result.setDefaultModel("msg", "1");
	                		}
	                	}else{
	                		money.setState(Integer.valueOf(state));
	                        money.setState_name(user.getNickname());
	                        money.setState_beizhu(state_beizhu);
	                        moneyDao.update(money);
	                        result.setDefaultModel("msg", "1");
	                	}
	                }
                } catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //缴费审核
    @RequestMapping(value = "/fp_state")
    public ModelAndView fp_state(ModelMap modelMap, final BaseVo vo,
                                 final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");
                String fp_state = request.getParameter("fp_state");

                TgUser user = UserUtil.getUserFromCookies();

                if (id != null && !"".equals(id)) {
                    TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
                    money.setFp_state(Integer.valueOf(fp_state));
                    money.setFp_state_name(user.getNickname());
                  
                    moneyDao.update(money);
                    result.setDefaultModel("msg", "1");
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //删除缴费
    @RequestMapping(value = "/deleteMoney")
    public ModelAndView deleteMoney(ModelMap modelMap, final BaseVo vo,
                                    final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("ids");

                TgUser user = UserUtil.getUserFromCookies();
                if (id != null && !"".equals(id)) {
                    TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
                    if (money != null) {
                        if (money.getUsername().equals(user.getUsername())) {
                            if (money.getState() != 2) {
                                moneyDao.delete(TgMoney.class, Long.valueOf(id));
                                result.setDefaultModel("msg", "1");
                            } else {
                                result.setDefaultModel("msg", "3");
                            }

                        } else {
                            result.setDefaultModel("msg", "2");
                        }
                    }
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //模糊搜索学生
    @RequestMapping(value = "/getStudentByName")
    public ModelAndView getStudentByName(ModelMap modelMap, final BaseVo vo,
                                         final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String nickname = request.getParameter("nickname");
               
                if (nickname != null && !"".equals(nickname)) {
                    TgStudent student = new TgStudent();
                    student.setNickname(nickname);
                    List<TgStudent> list = studentDao.getStudentByType(student);
                    Gson gson = new Gson();
                    for (TgStudent stu : list) {
                        //联系方式
                        if (stu.getPhone() != null && !"".equals(stu.getPhone())) {
                            if (stu.getPhone().contains("[{")) {
                                List<Phone> phone1 = gson.fromJson(stu.getPhone(),
                                        new TypeToken<List<Phone>>() {
                                        }.getType());
                                stu.setPhone(phone1.get(0).getPhone());
                            }
                        }
                    }
                    result.setDefaultModel("list", list);
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //发票审核列表
    @RequestMapping(value = "/fapiaoList")
    public ModelAndView fapiaoList(ModelMap modelMap, final BaseVo vo,
                                   final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center = request.getParameter("center");
                String stu_name = request.getParameter("stu_name");
                String state = request.getParameter("state");
                String nickname1 = request.getParameter("nickname1");
                String startTime = request.getParameter("startTime");
                String endTime = request.getParameter("endTime");

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                TgMoney money = new TgMoney();

                result.setDefaultModel("startTime", startTime);
                result.setDefaultModel("endTime", endTime);
                if (startTime != null && !"".equals(startTime)) {
                    try {
                        money.setStartTime(sdf.format(sdf.parse(startTime)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (endTime != null && !"".equals(endTime)) {
                    try {
                        money.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(sdf.format(sdf.parse(endTime))))));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (center != null && !"".equals(center) && !"undefined".equals(center)) {
                    money.setJd_center_key(center);
                }
                if (stu_name != null && !"".equals(stu_name)) {
                    money.setStu_name(stu_name);
                }
                if (state != null && !"".equals(state)) {
                    money.setState(Integer.valueOf(state));
                }
                if (nickname1 != null && !"".equals(nickname1)) {
                    money.setNickname1(nickname1);
                }

                Integer currPage = 1;
                if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
                    currPage = vo.getCurrPage();
                }
                Integer total = moneyDao.getMonCountFpByType(money);
                Page page = PageUtil.createPage(8, currPage, total);
                List<TgMoney> list = moneyDao.getMoneysFpByType(money, page);
                if (list != null) {
                    Gson gson = new Gson();
                    for (TgMoney m : list) {
                        //联系方式
                        if (m.getPhone() != null && !"".equals(m.getPhone())) {
                            if (m.getPhone().contains("[{")) {
                                List<Phone> phone1 = gson.fromJson(m.getPhone(),
                                        new TypeToken<List<Phone>>() {
                                        }.getType());
                                m.setPhlist(phone1);
                            }
                        }
                    }

                }

                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);

                result.setDefaultModel("offList", offList);

                result.setDefaultModel("money", money);
                result.setDefaultModel("list", list);
                result.setDefaultModel("page", page);
                return result;
            }
        });
        return new ModelAndView("system/finance/fapiaoList", modelMap);
    }

    //内部流水列表
    @RequestMapping(value = "/tealiushuiList")
    public ModelAndView liushuiList(ModelMap modelMap, final BaseVo vo,
                                    final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    TgUser user = UserUtil.getUserFromCookies();
                    String tea = "";
                    String tea_center = "";
                    tea = URLEncoder.encode(user.getNickname(), "utf-8");
                    tea_center = URLEncoder.encode(user.getRegions(), "utf-8");
                    
                    
                    String url = "http://www.yueqiao.org/web/teaweb/nbliushuiList.json?center=" +tea_center + "&tea_name=" + tea+ "&tea_id=" + user.getId()+ "&currPage=" + vo.getCurrPage();
                    String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

                    JSONArray listObj = JSONObject.fromObject(moneyList).getJSONArray("list");
                    JSONObject pageObj = JSONObject.fromObject(moneyList).getJSONObject("page");
                    if (listObj != null && listObj.size() > 0) {
                        HashMap<String, JSONArray> map = new HashMap<String, JSONArray>();
                        map.put("list", listObj);
                        result.setDefaultModel("list", map.get("list"));
                    }
                    Page page = new Page();
                    if (pageObj != null && !"".equals(pageObj)) {
                        Gson gson = new Gson();
                        String s = pageObj.toString();
                        page = gson.fromJson(s, Page.class);
                    }
                    result.setDefaultModel("page", page);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });

        return new ModelAndView("/system/finance/teaLiushuiList", modelMap);
    }

    @RequestMapping(value = "/addLiushui")
    public ModelAndView addLiushui(ModelMap modelMap, final BaseVo vo,
                                   final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");

                    if (id == null || "".equals(id) || "null".equals(id)) {
                        id = "";
                    }

                    String url = "http://www.yueqiao.org/web/teaweb/gotoLiuShuiById.json?id=" + id;
                    String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

                    JSONObject listObj = JSONObject.fromObject(moneyList).getJSONObject("lius");

                    HashMap<String, JSONObject> map = new HashMap<String, JSONObject>();
                    map.put("lius", listObj);
                    result.setDefaultModel("lius", map.get("lius"));

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });

        return new ModelAndView("/system/finance/add_liushui", modelMap);
    }

    @RequestMapping(value = "/addLiushui_go")
    public ModelAndView addLiushui_go(ModelMap modelMap, final BaseVo vo,
                                      final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");
                    String type = request.getParameter("type");
                    String tea = "";
                    String tea_center = "";
                    String date = request.getParameter("date");
                    String moneys = request.getParameter("moneys");
                    String zhaiyao = request.getParameter("zhaiyao");
                    String beizhu = request.getParameter("beizhu");
                    String fj_url = request.getParameter("fj_url");

                    TgUser user = UserUtil.getUserFromCookies();

                    tea = URLEncoder.encode(user.getNickname(), "utf-8");

                    tea_center = URLEncoder.encode(user.getRegions(), "utf-8");

                    if (date == null || "".equals(date) || "null".equals(date)) {
                        date = "";
                    } else {
                        date = URLEncoder.encode(date, "utf-8");
                    }

                    if (zhaiyao == null || "".equals(zhaiyao) || "null".equals(zhaiyao)) {
                        zhaiyao = "";
                    } else {
                        zhaiyao = URLEncoder.encode(zhaiyao, "utf-8");
                    }

                    if (beizhu == null || "".equals(beizhu) || "null".equals(beizhu)) {
                        beizhu = "";
                    } else {
                        beizhu = URLEncoder.encode(beizhu, "utf-8");
                    }
                    if (fj_url == null || "".equals(fj_url) || "null".equals(fj_url)) {
                    	fj_url = "";
                    } else {
                    	fj_url = URLEncoder.encode(fj_url, "utf-8");
                    }
                    String url = "http://www.yueqiao.org/web/teaweb/addLiuShuiById.json?id=" + id + "&is_has=1&type=" + type + "&tea_id=" + user.getId() + "&teacher=" + tea + "&tea_center=" + tea_center + "&date=" + date + "&moneys=" + moneys + "&zhaiyao=" + zhaiyao + "&beizhu=" + beizhu + "&fj_url=" + fj_url + "&currPage=" + vo.getCurrPage();
                    String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

                    String msg = JSONObject.fromObject(moneyList).getString("msg");
                    result.setDefaultModel("msg", msg);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }

    //内部流水审核
    @RequestMapping(value = "/liushuiList")
    public ModelAndView tealiushuiList(ModelMap modelMap, final BaseVo vo,
          final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                	String type = request.getParameter("type");
                	String type1 = request.getParameter("type1");
                    String center = request.getParameter("center");
                    String state = request.getParameter("state");

                    if (type == null || "".equals(type) || "null".equals(type)) {
                    	type = "";
                    }
                    if (center == null || "".equals(center) || "null".equals(center)) {
                        center = "";
                    }else{
                    	result.setDefaultModel("center", center);
                    }
                    if (state == null || "".equals(state) || "null".equals(state)) {
                    	state = "";
                    }
                    result.setDefaultModel("center", center);
                    center = URLEncoder.encode(center, "utf-8");
                    String url = "http://www.yueqiao.org/web/teaweb/nbliushui.json?sh=1&center=" + center + "&state=" +state+"&type="+type+"&currPage=" + vo.getCurrPage();
                    String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

                    JSONArray listObj = JSONObject.fromObject(moneyList).getJSONArray("list");
                    JSONArray listObj2 = JSONObject.fromObject(moneyList).getJSONArray("clist");
                    JSONObject pageObj = JSONObject.fromObject(moneyList).getJSONObject("page");
                    JSONObject yueObj = JSONObject.fromObject(moneyList).getJSONObject("yue");
                    if (listObj != null && listObj.size() > 0) {
                        HashMap<String, JSONArray> map = new HashMap<String, JSONArray>();
                        map.put("list", listObj);
                        result.setDefaultModel("list", map.get("list"));

                    }
                    if (listObj2 != null && listObj2.size() > 0) {
                        HashMap<String, JSONArray> map = new HashMap<String, JSONArray>();
                        map.put("clist", listObj2);
                        result.setDefaultModel("clist", map.get("clist"));

                    }
                    if(yueObj!=null && !yueObj.toString().equals("null")){
                    	HashMap<String, JSONObject> map = new HashMap<String, JSONObject>();
                        map.put("yue", yueObj);
                        result.setDefaultModel("yue", map.get("yue"));
                    }
                    
                    Page page = new Page();
                    if (pageObj != null && !"".equals(pageObj)) {
                        Gson gson = new Gson();
                        String s = pageObj.toString();
                        page = gson.fromJson(s, Page.class);
                    }
                    result.setDefaultModel("page", page);
                    result.setDefaultModel("type", type);
                    result.setDefaultModel("state", state);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });

        return new ModelAndView("/system/finance/LiushuiList", modelMap);
    }
    
    //流水记录下载
    @RequestMapping(value = "/liushuiListXz")
    public ModelAndView tealiushuiListxz(ModelMap modelMap, final BaseVo vo,
          final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
    	modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                	String type = request.getParameter("type");
                    String center = request.getParameter("center");
                    String state = request.getParameter("state");

                    if (type == null || "".equals(type) || "null".equals(type)) {
                    	type = "";
                    }
                    if (center == null || "".equals(center) || "null".equals(center)) {
                        center = "";
                    }else{
                    	result.setDefaultModel("center", center);
                    }
                    if (state == null || "".equals(state) || "null".equals(state)) {
                    	state = "";
                    }
                    center = URLEncoder.encode(center, "utf-8");
                    String url = "http://www.yueqiao.org/web/teaweb/nbliushuixz.json?sh=1&center=" + center + "&state=" +state+"&type="+type+"&currPage=" + vo.getCurrPage();
                    String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

                    JSONArray listObj = JSONObject.fromObject(moneyList).getJSONArray("list");
                    JSONObject yueObj = JSONObject.fromObject(moneyList).getJSONObject("yue");
                    if (listObj != null && listObj.size() > 0) {
                        String name = WriterExcel.writeExcelLiushuiXz(listObj,yueObj,center);
                        result.setDefaultModel("name", name);
                    }
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });

        return new ModelAndView("", modelMap);
    }


    //审核
    @RequestMapping(value = "/addState")
    public ModelAndView addState(ModelMap modelMap, final BaseVo vo,
        final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");
                String state = request.getParameter("state");
                String teacher = request.getParameter("teacher");
                try {
					teacher = URLEncoder.encode(teacher, "utf-8");
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                String url = null;
                if(teacher!=null && !"".equals(teacher)){
                	url = "http://www.yueqiao.org/web/teaweb/liushuiState.json?id=" + id + "&state=" + state+ "&teacher=" + teacher;
                }else{
                	url = "http://www.yueqiao.org/web/teaweb/liushuiState.json?id=" + id + "&state=" + state;
                }
                String moneyList = HttpUtil.doGet(url, "", "utf-8", true);
                String msg = JSONObject.fromObject(moneyList).getString("msg");
                result.setDefaultModel("msg", msg);
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }


    /**
     * 招生记录
     */

    @RequestMapping(value = "/enrollmentRecord")
    public ModelAndView enrollmentRecord(ModelMap modelMap, final BaseVo vo,
        final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, vo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String center = request.getParameter("center");
                String stu_name = request.getParameter("stu_name");
                String state = request.getParameter("state");
                String nickname1 = request.getParameter("nickname1");
                String startTime = request.getParameter("startTime");
                String endTime = request.getParameter("endTime");
                String stu_type = request.getParameter("stu_type");
                String status = request.getParameter("status");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                TgUser user = UserUtil.getUserFromCookies();

                TgMoney money = new TgMoney();
                if (stu_type == null || "".equals(stu_type)) {
                    money.setMoneyType1(1);//新增
                } else {
                    if (stu_type.equals("3")) {
                        money.setMoneyType1(null);
                        money.setPra_type(5);
                    } else if (stu_type.equals("4")){
                        money.setMoneyType1(null);
                       // money.setPra_type(7);
                        money.setSmf_m("1");
                    }else if (stu_type.equals("5")){
                    	//如果培优班旧缴费类型另算
                        money.setMoneyType1(null);
                        money.setPra_type(9);
                    }

                    else {
                        money.setMoneyType1(Integer.valueOf(stu_type));
                    }

                }

                if (!user.getUsername().equals("system")) {
                    if (user.getMenu() != null && !"".equals(user.getMenu())) {
                        if (!user.getMenu().contains("14")) {
                            if (user.getMenu().contains("y")) {
                                money.setJd_center_key(user.getRegion());
                            } else {
                                money.setUsername(user.getUsername());
                            }
                        }

                    } else {
                        money.setUsername(user.getUsername());
                    }
                }
                result.setDefaultModel("startTime", startTime);
                result.setDefaultModel("endTime", endTime);
                if (startTime != null && !"".equals(startTime)) {
                    try {
                        money.setStartTime(sdf.format(sdf.parse(startTime)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (endTime != null && !"".equals(endTime)) {
                    try {
                        money.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(sdf.format(sdf.parse(endTime))))));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                if (center != null && !"".equals(center) && !"undefined".equals(center)) {
                    money.setJd_center_key(center);
                }
                if (stu_name != null && !"".equals(stu_name)) {
                    money.setStu_name(stu_name);
                }
                if (state != null && !"".equals(state)) {
                    money.setState(Integer.valueOf(state));
                }
                if (nickname1 != null && !"".equals(nickname1)) {
                    money.setNickname1(nickname1);
                }
                Integer currPage = 1;
                if (vo.getCurrPage() != null && !"".equals(vo.getCurrPage())) {
                    currPage = vo.getCurrPage();
                }
                Integer total = moneyDao.getEnrollmentRecordCount(money);
                if (status != null && !"".equals(status) && status.equals("1")) {
                    Page page = PageUtil.createPage(total, currPage, total);
                   
                    List<TgMoney> list = moneyService.getEnrollmentRecordForExcel(money, page);
                    //excel导出
                    try {
                        WriterExcel.writeExcelenrollmentRecord(list, response, stu_type,moneyDao);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                Page page = PageUtil.createPage(8, currPage, total);
                List<TgMoney> list = moneyDao.getEnrollmentRecord(money, page);           
                if (list != null) {
                    Gson gson = new Gson();
                    for (TgMoney m : list) {
                        //联系方式
                        if (m.getPhone() != null && !"".equals(m.getPhone())) {
                            if (m.getPhone().contains("[{")) {
                                List<Phone> phone1 = gson.fromJson(m.getPhone(),
                                        new TypeToken<List<Phone>>() {
                                        }.getType());
                                m.setPhlist(phone1);
                            }
                        }List<TgMoney> depositDate = null;
                        if (m.getStu_id()!=null && m.getStu_id()!=0){
                                //根据id查询定金日期
                            TgMoney money2 = new TgMoney();
                            money2.setStu_id(m.getStu_id());
                           depositDate = moneyDao.getDepositDate(money2);
                        }else if (!StringUtil.isEmpty(m.getStu_name())){
                                //根据姓名查询定金日期
                            TgMoney money2 = new TgMoney();
                            money2.setStudent(m.getStudent());
                             depositDate = moneyDao.getDepositDate(money2);
                        }if (depositDate!=null && depositDate.size()>0){
                            m.setDepositDate(depositDate.get(0).getMoneyDate());
                        }
                    }
                }
                TgOffice office = new TgOffice();
                List<TgOffice> offList = tgOfficeDao.getOfficeName(office);
                result.setDefaultModel("offList", offList);
                result.setDefaultModel("money", money);
                result.setDefaultModel("list", list);
                result.setDefaultModel("page", page);
                result.setDefaultModel("stu_type", stu_type);
                result.setDefaultModel("center", center);
                result.setDefaultModel("stu_name", stu_name);
                result.setDefaultModel("state", state);
                result.setDefaultModel("nickname1", nickname1);

                return result;
            }
        });
        return new ModelAndView("system/finance/enrollment_record", modelMap);
    }

    //咨询师个数绩效统计
    @RequestMapping(value = "/cul_count_statis")
    public ModelAndView cul_count_statis(ModelMap modelMap, final LoginVo loginVo,
          final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = WebExecutor.execute(logger, loginVo, new Biz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String startTime = request.getParameter("startTime");
                    String endTime = request.getParameter("endTime");

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    
                    if(startTime==null || "".equals(startTime) ||
                    		endTime==null || "".equals(endTime)){
                    	return result;
                    }
                    
                    List<ConsCount> list = new ArrayList<ConsCount>();
                    TgUserinfo user = new TgUserinfo();
                    user.setJob("课程顾问");
                    List<TgUserinfo> uList = userinfoDao.getUserInfoByUserInfo(user);
                    if (uList != null && uList.size() > 0) {
                        TgMoney money = new TgMoney();
                        for (TgUserinfo u : uList) {
                        	ConsCount con = new ConsCount();
                        	con.setNickname(u.getNickname());
                        	con.setCenter(u.getRegions());
                        	startTime = request.getParameter("startTime");

                        	if(u.getZhuanz_date()!=null && !"".equals(u.getZhuanz_date())){
                        	

                        	boolean bFlag =	DateUtil.compareDate(startTime, u.getZhuanz_date());
                        	boolean aFlag = DateUtil.compareDate(endTime, u.getZhuanz_date());
                        	if(!bFlag && aFlag){
                              long dayCount =		DateUtil.getDistanceDays(startTime, u.getZhuanz_date());//获取非转正天数
                             // startTime = u.getZhuanz_date();
                              con.setDayCount(Long.toString(dayCount));
                        	} 
                        	con.setZhuanz_date(u.getZhuanz_date());
                        	} else{
                        		con.setZhuanz_date("9999-01-01");	
                        	}
                        	
                                money.setJd_username1(u.getUsername());
                        
                              //  money.setState(2);
                                money.setStartTime(startTime);
                                money.setEndTime(sdf.format(DateUtil.getTommorrowDay1(sdf.parse(endTime))));
                                money.setMoneyType1(1);
                                List<TgMoney> mlist = moneyDao.getMoneysTidianByType(money);
                                
                                List<TgMoney> dlist = moneyDao.getMoneysTidianDingjinByType(money);
                               
                                
                                if (mlist != null && mlist.size() > 0) {
                                	List<Moneys> monyslist = new ArrayList<Moneys>();
                                    for (TgMoney m : mlist) {
                                    	
                                    	Double tims = Double.valueOf(m.getTimes());
                                    	Moneys moneys = new Moneys();
                                    	moneys.setMoneyDate(m.getMoneyDate());
                                    	if(m.getStu_name()!=null && !"".equals(m.getStu_name())){
                                    		moneys.setStu_name(m.getStu_name());
                                    	}else{
                                    		moneys.setStu_name(m.getStudent());
                                    	}
                                    	moneys.setStu_name(m.getStu_name());
                                    	double cc = 1.0;
                                    
                                    	if( m.getFp_moneys() != null &&  m.getFp_moneys() >0){
                                    	cc = m.getMoneys()/m.getFp_moneys();	
                                    	}
                                            	
                                       
                                       
                                    	if(m.getPra_type()==1){
                                			moneys.setKc_msg("非脑电"+tims+"次");
                                			moneys.setKc_count((tims/120)*1.0 *cc);
                                    	}else if(m.getPra_type()==2){
                                    		moneys.setKc_msg("普通中心"+tims+"次");
                                    		moneys.setKc_count((tims/60)*1.0 *cc);
                                    	}else if(m.getPra_type()==3){
                                    		moneys.setKc_msg("全脑"+tims+"次");
                                    		moneys.setKc_count((tims/45)*1.0*cc);
                                    	}else if(m.getPra_type()==4){
                                    		if(m.getTimes()<=30){//体验包
                                    			moneys.setKc_msg("太优"+tims+"次");
                                        		moneys.setKc_count(0.5);
                                    		}else{
                                    			moneys.setKc_msg("太优"+tims+"次");
                                        		moneys.setKc_count((tims/60)*1.0*cc);
                                    		}
                                    	}else if(m.getPra_type()==8){
                                    		moneys.setKc_msg("暑期培优班(非在读)"+tims+"次");
                                    		moneys.setKc_count(0.25);
                                    	}
                                    	moneys.setPra_type(m.getPra_type());
										moneys.setMoneys(m.getMoneys());

                                    	//加上之前没有统计的定金
									if (dlist.size() > 0 && dlist != null) {
										for (TgMoney m1 : dlist) {
											if (m.getStu_id().equals( m1.getStu_id()) ) {
												moneys.setMoneys(m.getMoneys()
														+ m1.getMoneys());
											}
										}

									} 
                                       
                                    	
                                    	
                                    	monyslist.add(moneys);
                                    }
                                    con.setList(monyslist);
                                   
                                }
                                list.add(con);
                        	
                        }
                    }
                    result.setDefaultModel("list", list);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("system/finance/statis/sys_geshu", modelMap);
    }






    //咨询师预约到访下载
    @RequestMapping(value = "/enrollmentSingular")
    public ModelAndView enrollmentSingular(ModelMap modelMap, final BaseVo vo,
    		final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String startTime = request.getParameter("startTime");
                    String endTime = request.getParameter("endTime");
                    List<TgOffice> officeList = tgOfficeDao.getOfficeName(new TgOffice());
                    String name = WriterExcel.writeExcelSingular(officeList,startTime, endTime,receptionDao,tgOfficeDao,moneyService);
                    result.setDefaultModel("name", name);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }


    /**
     * 标记是否属于是成单
     */
    @RequestMapping(value = "/isSuccessOrder")
    public void enrollmentSingular(ModelMap modelMap, HttpServletRequest request,  HttpServletResponse response)
            throws Exception {
                PrintWriter writer = response.getWriter();
                try {
                    String ids = request.getParameter("ids");
                    String flag = request.getParameter("flag");
                    if(!StringUtil.isEmpty(ids)){
                        TgMoney tgMoney = new TgMoney();
                        String[] id = ids.split(",");
                        for (String moneyId: id) {
                            tgMoney.setId(Long.valueOf(moneyId));
                            if (flag.equals("1")){
                                tgMoney.setIsSuccess(1);
                            }else {
                                tgMoney.setIsSuccess(2);
                            }moneyDao.update(tgMoney);
                        }
                        writer.print("success");
                    }

                } catch (Exception e) {
                    writer.print("error");
                }finally {
                    writer.close();
                }
    }

    //流水信息下载
    @RequestMapping(value = "/flowInformation")
    public ModelAndView flowInformation(ModelMap modelMap, final BaseVo vo,
                                           final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String startTime = request.getParameter("startTime");
                    String endTime = request.getParameter("endTime");
                    TgMoney tgMoney =new TgMoney();
                   //流水下载加入user所属接待中心条件 
                    TgUser user = UserUtil.getCookieUser();
                    if (!user.getUsername().equals("system")) {
                        if (user.getMenu() != null && !"".equals(user.getMenu())) {
                            if (!user.getMenu().contains("14")) {
                                if (user.getMenu().contains("y")) {
                                    tgMoney.setJd_center_key(user.getRegion());
                                } else {
                                    tgMoney.setUsername(user.getUsername());
                                }
                            }

                        } else {
                            tgMoney.setUsername(user.getUsername());
                        }
                    }
                    
                    tgMoney.setStartTime(startTime);
                    tgMoney.setEndTime(endTime);
                    List<TgMoney> moneyList = moneyDao.getFlowInformation(tgMoney);
                    tgMoney.setCenter("太优杭州学院路中心");
                    List<TgMoney> moneyList2 = moneyDao.getFlowInformation(tgMoney);
                    String name = WriterExcel.writeFlowInformation(moneyList,moneyList2);
                    result.setDefaultModel("name", name);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }
  //文件批量下载  
    @RequestMapping(value = "/getpack")
	public ModelAndView shapan(ModelMap modelMap, final BaseVo vo,
			final HttpServletRequest request, final HttpServletResponse response)
			throws Exception {
		modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");

				// 文件照片
				TgMoney money = new TgMoney();

				money = moneyDao.findById(TgMoney.class, Long.valueOf(id));

				if (money != null) {
					List<String> a_list = new ArrayList<String>();
//凭证打包
					String[] aa = money.getPz_url().split("\\,");
					for (int j = 0; j < aa.length; j++) {
						if (aa[j] != null && !"".equals(aa[j])) {
							File[] file1 = { new File(aa[j]) };
							String fileP = aa[j];
                            String filePt = fileP.substring(fileP.lastIndexOf("/")+1, fileP.indexOf(".")); 
							String strZipName =  filePt;

							strZipName = strZipName.replace(".jpg", "");
							strZipName = strZipName.replace(".JPG", "");
							strZipName = strZipName.replace(".gif", "");
							strZipName = strZipName.replace(".jpeg", "");
							strZipName = strZipName.replace(".png", "");
							strZipName = strZipName.replace(".rar", "");
							strZipName = strZipName.replace(".zip", "");
							strZipName = strZipName.replace(".PNG", "")
									+ ".zip";
							// 打包
							String name = ZipOutput
									.getZipOut(strZipName, file1);
							a_list.add("/mydata/uploadtea/pack/" + name);
						}

					}
		//服务协议打包			
					String[] fw = money.getFw_url().split("\\,");
					for (int i = 0; i < fw.length; i++) {
						if (fw[i] != null && !"".equals(fw[i])) {
							File[] file1 = { new File(fw[i]) };
							
							 
						 String strZipName = "服务协议-"+String.valueOf(i);;

							strZipName = strZipName.replace(".jpg", "");
							strZipName = strZipName.replace(".JPG", "");
							strZipName = strZipName.replace(".gif", "");
							strZipName = strZipName.replace(".jpeg", "");
							strZipName = strZipName.replace(".png", "");
							strZipName = strZipName.replace(".rar", "");
							strZipName = strZipName.replace(".zip", "");
							strZipName = strZipName.replace(".PNG", "")
									+ ".zip";
							// 打包
							String name = ZipOutput
									.getZipOut(strZipName, file1);
							a_list.add("/mydata/uploadtea/pack/" + name);
						}

					}

					if (a_list != null && a_list.size() > 0) {
						File[] file1 = new File[a_list.size()];
						TgStudent stu = studentDao.findById(TgStudent.class, Long.valueOf(money.getStu_id()));
						String strZipName = stu.getNickname() + "-"+ String.valueOf(money.getId())+ ".zip";
						for (int i = 0; i < a_list.size(); i++) {
							// 打包下载
							if (a_list.get(i) != null
									&& !"".equals(a_list.get(i))) {
								file1[i] = new File(a_list.get(i));
							} else {
								file1[i] = null;
							}
						}
						// 打包下载

						String name = "/mydata/uploadtea/pack/"
								+ ZipOutput.getZipOut(strZipName, file1);
						result.setDefaultModel("name", name);
					}
				}

				return result;
			}
		});

		return new ModelAndView("", modelMap);
	}
   
    
    //根据学员id或者名字查询训练记录
    @RequestMapping(value = "/getRecordByType")
    public ModelAndView getRecordByType(ModelMap modelMap, final BaseVo vo,
       final HttpServletRequest request, final HttpServletResponse response)
            throws Exception {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                    String id = request.getParameter("id");
                    String name = request.getParameter("name");
                    
                    if(name!=null && !"".equals(name)){
                    	name = URLEncoder.encode(name, "utf-8");
                    }
					String url = "http://www.yueqiao.org/web/teaweb/getRecordByStuIdOrStuName.json?id="+id+"99&name="+name;
					String content = HttpUtil.doGet(url, "", "utf-8", true);
					
					String msg = JSONObject.fromObject(content).getString("msg");
					if(msg.equals("1")){
						String count = JSONObject.fromObject(content).getString("count");
						result.setDefaultModel("count", count);
					}else{
						result.setDefaultModel("count", "0");
					}
                    result.setDefaultModel("msg", msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        return new ModelAndView("", modelMap);
    }
    //删除缴费，无条件限制
    @RequestMapping(value = "/tsdeleteMoney")
    public ModelAndView tsdeleteMoney(ModelMap modelMap, final BaseVo vo,
                                    final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("id");

                TgUser user = UserUtil.getUserFromCookies();
                if (id != null && !"".equals(id)) {
                    TgMoney money = moneyDao.findById(TgMoney.class, Long.valueOf(id));
                    if (money != null) {
                        if (user.getUsername().equals("he")) {
                            moneyDao.delete(TgMoney.class, Long.valueOf(id));
                            result.setDefaultModel("msg", "1");
                        } else {
                            result.setDefaultModel("msg", "2");
                        }
                    }
                }
                return result;
            }
        });
        return new ModelAndView("moneyList", modelMap);
    }
    
    
    
  //快速查找训练老师列表
    @RequestMapping(value = "/tuifeiindex")
    public ModelAndView tuifeiindex(ModelMap modelMap, final BaseVo vo,
       final HttpServletRequest request, HttpServletResponse response)
            throws BaseException {
        modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
            public Result process() {
                Result result = new ResultSupport(true);
                try {
                	String nickname = request.getParameter("nickname");
                	String stuName = nickname;//临时缓存name 到后面会换编码格式。
                	TgUser user = UserUtil.getUserFromCookies();
                	if(nickname!=null && !"".equals(nickname)){
                		nickname=URLEncoder.encode(nickname, "utf-8");
                	}else{
                		nickname="";
                	}
                	
                		String teaid=user.getId().toString();
                		
                		String url = "http://www.yueqiao.org/web/teaweb/tuifeiindex.json?name="+nickname+"&teaid="+teaid;
                		String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

                        JSONArray listObj = JSONObject.fromObject(moneyList).getJSONArray("list");
                        JSONObject pageObj = JSONObject.fromObject(moneyList).getJSONObject("page");
                         
                        if (listObj != null && listObj.size() > 0) {
        					List<TuiFei> list=new ArrayList();
        					for(int i=0;i<listObj.size();i++) {
        						JSONObject obj= JSONObject.fromObject(listObj.get(i));
        						TuiFei tuifei=new TuiFei();
        						tuifei.setId(obj.getLong("id"));
        						tuifei.setStu_id(obj.getLong("stu_id"));
        						tuifei.setStu_name(obj.getString("stu_name"));
        						tuifei.setTuifei_date(obj.getString("tuifei_date"));
        						tuifei.setCourse_type(obj.getString("course_type"));
        						tuifei.setMoney(obj.getString("money").equals("null")?"0":obj.getString("money"));
        						tuifei.setBeizhu(obj.getString("beizhu"));
        						tuifei.setState(obj.getString("state"));
        						list.add(tuifei);
        					}
        					result.setDefaultModel("tlist",list);
                        }
                        Page page = new Page();
                        if (pageObj != null && !"".equals(pageObj)) {
                            Gson gson = new Gson();
                            String s = pageObj.toString();
                            page = gson.fromJson(s, Page.class);
                        }
                        result.setDefaultModel("stu_name",stuName );
				} catch (Exception e) {
					e.printStackTrace();
				}
                return result;
            }
        });
        return new ModelAndView("system/finance/tuifei", modelMap);
    }
    
    //添加退费定金
  @RequestMapping(value = "/addtuifei")
  public ModelAndView addtuifei(ModelMap modelMap, final BaseVo vo,
          final HttpServletRequest request, HttpServletResponse response)
        		  throws BaseException {
	modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
		public Result process() {
              Result result = new ResultSupport(true);
              try {
            	  String id=request.getParameter("id");
				   String tuifei_date=request.getParameter("tuifei_date");
				   String stu_id=request.getParameter("stu_id");
				   String stu_name=request.getParameter("student");
			       String course_type=request.getParameter("course_type");
			       String money=request.getParameter("money");
			       String beizhu=request.getParameter("beizhu");
			       String tea_word=request.getParameter("tea_word");
			       String jz_word=request.getParameter("jz_word");
			       String state=request.getParameter("state");

              TgUser user = UserUtil.getUserFromCookies();


              if (tuifei_date == null || "".equals(tuifei_date) || "null".equals(tuifei_date)) {
            	  tuifei_date = "";
              } else {
            	  tuifei_date = URLEncoder.encode(tuifei_date, "utf-8");
              }

              if (course_type == null || "".equals(course_type) || "null".equals(course_type)) {
            	  course_type = "定金";
              } else {
            	  course_type = URLEncoder.encode(course_type, "utf-8");
              }
              if (stu_name == null || "".equals(stu_name) || "null".equals(stu_name)) {
            	  stu_name = "";
              } else {
            	  stu_name = URLEncoder.encode(stu_name, "utf-8");
              }

              if (beizhu == null || "".equals(beizhu) || "null".equals(beizhu)) {
                  beizhu = "";
              } else {
                  beizhu = URLEncoder.encode(beizhu, "utf-8");
              }
              result.setDefaultModel("state", state);
              if (state == null || "".equals(state) || "null".equals(state)) {
            	  state = "";
              } else {
            	  state = URLEncoder.encode(state, "utf-8");
              }
              String url = "http://www.yueqiao.org/web/teaweb/saveTuifei.json?id=" + id + 
            		  "&type=2&stu_name=" + stu_name + 
            		  "&teaid=" + user.getId() + 
            		  "&stu_id=" + stu_id + 
            		  "&jz_word=" + jz_word + 
            		  "&tea_word=" + tea_word +
            		  "&tuifei_date=" + tuifei_date + 
            		  "&course_type=" + course_type + 
            		  "&beizhu=" + beizhu + 
            		  "&state=" + state + 
            		  "&moneys=" + money;
              String moneyList = HttpUtil.doGet(url, "", "utf-8", true);

              result.setDefaultModel("msg", 1);
              
              
              } catch (UnsupportedEncodingException e) {
  				e.printStackTrace();
  			}
              return result;
          }
      });
      return new ModelAndView("", modelMap);
  }
  
  
  	@RequestMapping(value = "/tuifeidetail")
	public ModelAndView tuifeidetail(ModelMap modelMap, final BaseVo vo,
			final HttpServletRequest request, final HttpServletResponse response)
			throws Exception {
		modelMap = WebExecutor.execute(logger, vo, new Biz() {
			public Result process() {
				Result result = new ResultSupport(true);
				String id = request.getParameter("id");
				 try {
	                  if (id != null && !"".equals(id)) {
	                    String url = "http://www.yueqiao.org/web/teaweb/tuifeidetail.json?id=" +id;
	 	                String tufList = HttpUtil.doGet(url, "", "utf-8", true);
	 	                JSONObject listObj = JSONObject.fromObject(tufList).getJSONObject("tuifei");
	 	                if(listObj.getString("money").equals("null")) {
	 	                	listObj.element("money", "0");
	 	                }
	       				result.setDefaultModel("tuf",listObj);
	 	              }
	                } catch (Exception e) {
	                    e.printStackTrace();
	                }
				return result;
			}
		});
		
		return new ModelAndView("system/finance/addTuifei", modelMap);
	}
  	
  	 //删除退费信息
    @RequestMapping(value = "/deleteTuifei")
    public ModelAndView deleteTuifei(ModelMap modelMap, final BaseVo vo,
            final HttpServletRequest request, HttpServletResponse response)
          		  throws BaseException {
  	modelMap = JsonExecutor.execute(logger, vo, new JsonBiz() {
  		public Result process() {
                Result result = new ResultSupport(true);
                String id = request.getParameter("ids");
                TgUser user = UserUtil.getUserFromCookies();
  				try {
  					if (id != null && !"".equals(id)) {
  						String url = "http://www.yueqiao.org/web/teaweb/deleteTuifei.json?id=" +id+"&teaid="+user.getId();
//  						String url = "http://127.0.0.1:8081/web/teaweb/deleteTuifei.json?id=" +id+"&teaid="+user.getId();
  						String objStr = HttpUtil.doGet(url, "", "utf-8", true);
  						String msg=JSONObject.fromObject(objStr).getString("msg");
  						result.setDefaultModel("msg",msg);
  					}
  				} catch (Exception e) {
  					e.printStackTrace();
  				}
  				return result;
            }
        });
        return new ModelAndView("", modelMap);
    }
}
